import { getNodeButtons, getOneTargetProcess } from "@/http/api";
import { en, getContent } from "./locale"
import dayjs from "dayjs";
export default {
  // 日期格式化
  formatDate(_date) {
    let year = _date.getFullYear()
    let month = _date.getMonth() + 1
    let date = _date.getDate()
    let hours = _date.getHours()
    let minutes = _date.getMinutes()
    let seconds = _date.getSeconds()
    return `${year}-${month < 10 ? '0' + month : month}-${date < 10 ? '0' + date : date} ${
      hours < 10 ? '0' + hours : hours
    }:${minutes < 10 ? '0' + minutes : minutes}:${seconds < 10 ? '0' + seconds : seconds}`
  },

  // 复制
  async copy(data) {
    await navigator.clipboard.writeText(data)
  },

  $t(defaut, pathString) {
    let clocale = JSON.parse(localStorage.getItem("locale"))
    if(clocale !== 'zh') {
      let path = pathString.split(".")
      let res = ""
      for(let i = 0; i < path.length; i++) {
        res = getContent(i ? res : en, path[i])
      }
      return res
    }
    return defaut
  },

  changeTime(lists) {
    lists.forEach((element) => {
      element.create_time_analysis = dayjs(element.create_time).format(
        "YY-MM-DD"
      );
    });
    return lists;
  },

  cancel(_this) {
    _this.$confirm("放弃保存当前表单内？", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
      callback: async (action) => {
        if (action === "confirm") {
          _this.$router.back()
        }
      },
    });
  },

  getUrl(app_id) {
    switch (app_id) {
      case "KPI":
        return "/proportal/examine";
      case "handle_over":
        return "/proportal/handover";
      case "formal_apply":
        return "/proportal/formalapply";
      case "run_apply":
        return "/proportal/runapply";
      case "material":
        return "/proportal/material";
      case "seal":
        return "/proportal/seal";
      case "remote":
        return "/proportal/remote";
      case "holiday":
        return "/proportal/holiday";
      default:
        return ""
    }
  },

  getProcessIdOptions(_this, res) {
    if(res.data.app_id_number && Object.keys(res.data.app_id_number).length > 0) {
      _this.processIdOptions = [
        {
          label: this.$t("全部", "processCenter.allApp"),
          value: "all",
          num: res.data.rows.length,
        },
      ];
      for (const key in res.data.app_id_number) {
        let value = res.data.app_id_number[key];
        _this.processIdOptions.push({
          label: Object.keys(value)[0],
          value: key,
          num: value[Object.keys(value)[0]],
        });
      }
    } else {
      _this.app_id = ""
    }
  },

  async ifHasAuth(_this, mode, processId, operate_node) {
    if(mode) {
      let currentUser = JSON.parse(localStorage.getItem("userInfo"))
      if(processId) {
        let res = await getOneTargetProcess({process_id: processId, operate_node})
        if(res.code === 20041) {
          if((mode === 'submit' || mode === 'my_apply') && processId) {
            if(res.data.submit_employee_id === currentUser.employee_id) {
              return
            } else {
              _this.$router.replace('/403')
            }
          } else if(mode === 'my_todo') {
            if(!res.data.current_task_handler.split(",").includes(currentUser.employee_id)) {
              _this.$router.replace('/403')
            }
          } else if(mode === "have_done") {
            if(!res.data.operator.includes(currentUser.employee_id)) {
              _this.$router.replace('/403')
            }
          }
        } 
      }
    } else {
      _this.$router.replace('/404')
    }
  },

  async getNodeButtons(_this, mode, processId, operate_node) {
    if(mode === "submit") {
      return [
        {
          label: this.$t('取消', 'processCenter.cancel'),
          value: 'cancel',
          primary: false
        },
        {
          label: this.$t('保存草稿', 'processCenter.toDraft'),
          value: 'draft',
          primary: false
        },
        {
          label: this.$t('提交', 'processCenter.submit'),
          value: 'submit',
          primary: true
        }
      ]
    } else {
      let res = await getNodeButtons({
        process_id: processId,
        mode,
        operate_node
      })
      if(res.code === 20041) {
        let allNodes = [
          {
            label: this.$t("同意", 'processCenter.agree'),
            value: 'Approve',
            back: 'agree',
            primary: true
          },
          {
            label: this.$t("拒绝", 'processCenter.refuse'),
            value: 'Reject',
            back: 'refuse',
            primary: true
          },
          {
            label: this.$t("知会", 'processCenter.notice'),
            value: 'Notify',
            back: 'tell',
            primary: false
          },
          {
            label: this.$t("撤回", 'processCenter.recall'),
            value: 'Revoke',
            back: 'recall',
            primary: true
          }
        ]
        return allNodes.filter(item => {
          return res.data.includes(item.back)
        })
      }
    }
  },
}
  